package top.xbaoziplus.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import top.xbaoziplus.company.vo.PositionVo;
import top.xbaoziplus.company.common.Result;
import top.xbaoziplus.company.empty.Position;
import top.xbaoziplus.company.service.PositionService;
import top.xbaoziplus.company.mapper.PositionMapper;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position>
    implements PositionService{
    @Autowired
    PositionMapper positionMapper;


    /**
     * @description 新增职位
     * @author kang
     * @date 2022/6/29 下午 3:41
     * @param position 插入的职位
     * @return top.xbaoziplus.company.common.Result 结果
     */
    @Override
    public Result add(Position position) {
        //判断参数是否有误
        if (ObjectUtils.isEmpty(position)){
            return Result.fail("参数错误");
        }
        //判断职位是否存在
        LambdaQueryWrapper<Position> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Position::getName, position.getName());
        Position position1 = positionMapper.selectOne(queryWrapper);
        if (!ObjectUtils.isNull(position1)){
            return Result.fail("职位已存在");
        }
        //职位不存在则新增
        int insert = positionMapper.insert(position);
        if (insert > 0){
            return Result.success("新增成功");
        }
        return Result.fail("新增失败");
    }

    /**
     * @description 恢复职位ID
     * @author kang
     * @date 2022/6/30 上午 8:51
     * @param id 职位ID
     * @return top.xbaoziplus.company.common.Result
     */
    @Override
    public Result recovery(Integer id) {
        if (ObjectUtils.isNull(id)){
            return Result.fail("参数错误");
        }
        Boolean aBoolean = positionMapper.updateStatusById(id);
        if (aBoolean){
            return Result.success("恢复职位成功");
        }
        return Result.fail("恢复职位失败");
    }

    /**
     * @description 分页展示职位信息
     * @author kang
     * @date 2022/6/30 上午 8:50
     * @param positionVo 前端传来的数据
     * @return top.xbaoziplus.company.common.Result
     */
    @Override
    public Result listPage(PositionVo positionVo) {
        if (ObjectUtils.isNull(positionVo.getCurrentPage()) || ObjectUtils.isNull(positionVo.getSize())){
            return  Result.fail("参数错误");
        }
        LambdaQueryWrapper<Position> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Position::getStatus, 0);
        //如果name不为空
        if (!StringUtils.isBlank(positionVo.getName())){
            queryWrapper.like(Position::getName, positionVo.getName());
        }
        Page<Position> positionPage = positionMapper.selectPage(new Page<Position>(positionVo.getCurrentPage(), positionVo.getSize()), queryWrapper);
        return Result.success(positionPage, "职位分页展示成功");
    }

    @Override
    public Result listDeletedPage(PositionVo positionVo) {
        if (ObjectUtils.isNull(positionVo.getCurrentPage()) || ObjectUtils.isNull(positionVo.getSize())){
            return  Result.fail("参数错误");
        }
        LambdaQueryWrapper<Position> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Position::getStatus, 1);
        //如果name不为空
        if (!StringUtils.isBlank(positionVo.getName())){
            queryWrapper.like(Position::getName, positionVo.getName());
        }
        Page<Position> positionPage = positionMapper.selectPage(new Page<Position>(positionVo.getCurrentPage(), positionVo.getSize()), queryWrapper);
        return Result.success(positionPage, "职位分页展示成功");
    }
}




